package com.aaa.cs.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.cs.dao.BrandDao;
import com.aaa.cs.entity.Brand;
import com.aaa.cs.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Brand)表服务实现类
 *
 * @author makejava
 * @since 2020-11-18 15:45:31
 */
@Service("brandService")
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandDao brandDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Long id) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.brandDao.queryById(id));
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Brand> queryAllByLimit(int offset, int limit) {
        return this.brandDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand insert(Brand brand) {
        this.brandDao.insert(brand);
        return brand;
    }

    /**
     * 修改数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand update(Brand brand) {
        this.brandDao.update(brand);
        return (Brand) this.queryById(brand.getId()).getData();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Long id) {

        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.brandDao.deleteById(id));
    }

    @Override
    public Result queryAll() {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.brandDao.queryAll(new Brand()));
    }


    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, Brand brand) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Brand> pageInfo = new PageInfo<Brand>(brandDao.queryAll(brand));
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

    /**
     * 新增或修改数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Result insertOrUpdate(Brand brand) {
        int i = 0;
        if (brand.getId() != null && brand.getId() !=0) {
            i = brandDao.update(brand);
            if (i>0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"修改成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"修改失败");
            }
        }else {
            i = brandDao.insert(brand);
            if (i >0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"添加成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"添加失败");
            }
        }
    }
}